package com.define.ssh.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.springframework.core.io.ClassPathResource;

import com.define.ssh.bean.Setting;

/**
 * 工具类 - 系统配置
 */

public class SettingUtil {

	private static final String SHOPXX_XML_FILE_NAME = "readboywx.xml";// customer.xml配置文件名称

	/***
	 * 获取dishes文件中某个节点的值
	 * 
	 * @param nodeName
	 * @return
	 * @throws URISyntaxException
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static String getSettingByNode(String nodeName)
			throws URISyntaxException, DocumentException, IOException {
		final File shopxxXmlFile = new ClassPathResource(SHOPXX_XML_FILE_NAME)
				.getFile();
		final SAXReader saxReader = new SAXReader();
		final Document document = saxReader.read(shopxxXmlFile);
		final Node node = document.selectSingleNode("/readboywx/setting/"
				+ nodeName);
		return node.getText();
	}

	/**
	 * 读取系统配置信息
	 * 
	 * @return Setting
	 * 
	 * @throws URISyntaxException
	 * 
	 * @throws DocumentException
	 * 
	 * @throws IOException
	 */
	public static Setting readSetting() throws URISyntaxException,
			DocumentException, IOException {
		final File shopxxXmlFile = new ClassPathResource(SHOPXX_XML_FILE_NAME)
				.getFile();
		final SAXReader saxReader = new SAXReader();
		final Document document = saxReader.read(shopxxXmlFile);
		final Node proNameNode = document
				.selectSingleNode("/readboywx/setting/proName");
		final Node copyrightNode = document
				.selectSingleNode("/readboywx/setting/copyright");
		final Node emailNode = document
				.selectSingleNode("/readboywx/setting/email");
		final Node assignOrderNode = document
				.selectSingleNode("/readboywx/setting/assignOrder");

		final Node img1Node = document
				.selectSingleNode("/readboywx/setting/img1");
		final Node img2Node = document
				.selectSingleNode("/readboywx/setting/img2");
		final Node img3Node = document
				.selectSingleNode("/readboywx/setting/img3");

		final Node adminimg1Node = document
				.selectSingleNode("/readboywx/setting/adminimg1");
		final Node adminimg2Node = document
				.selectSingleNode("/readboywx/setting/adminimg2");
		final Node adminimg3Node = document
				.selectSingleNode("/readboywx/setting/adminimg3");

		final Setting setting = new Setting();
		if (proNameNode != null) {
			setting.setProName(proNameNode.getText());
		}
		if (copyrightNode != null) {
			setting.setCopyright(copyrightNode.getText());
		}
		if (emailNode != null) {
			setting.setEmail(emailNode.getText());
		}
		if (assignOrderNode != null) {
			setting.setAssignOrder(assignOrderNode.getText());
		}

		if (img1Node != null) {
			setting.setImg1(img1Node.getText());
		}
		if (img2Node != null) {
			setting.setImg2(img2Node.getText());
		}
		if (img3Node != null) {
			setting.setImg3(img3Node.getText());
		}

		if (adminimg1Node != null) {
			setting.setAdminimg1(adminimg1Node.getText());
		}
		if (adminimg2Node != null) {
			setting.setAdminimg2(adminimg2Node.getText());
		}
		if (adminimg3Node != null) {
			setting.setAdminimg3(adminimg3Node.getText());
		}

		return setting;
	}

	/**
	 * 获取系统配置信息
	 * 
	 * @return Setting
	 */
	public static Setting getSetting() {
		Setting setting = null;
		try {
			setting = readSetting();
		} catch (final Exception e) {
			e.printStackTrace();
		} finally {
		}
		return setting;
	}

	/**
	 * 写入系统配置信息
	 * 
	 * @return Setting
	 */
	public static void writeSetting(Setting setting) {
		File shopxxXmlFile = null;
		Document document = null;
		try {
			shopxxXmlFile = new ClassPathResource(SHOPXX_XML_FILE_NAME)
					.getFile();
			final SAXReader saxReader = new SAXReader();
			document = saxReader.read(shopxxXmlFile);
		} catch (final Exception e) {
			e.printStackTrace();
		}
		final Element rootElement = document.getRootElement();
		final Element settingElement = rootElement.element("setting");
		Node proNameNode = document
				.selectSingleNode("/readboywx/setting/proName");
		if (proNameNode == null) {
			proNameNode = settingElement.addElement("proName");
		}
		proNameNode.setText(setting.getProName());

		Node copyrightNode = document
				.selectSingleNode("/readboywx/setting/copyright");
		if (copyrightNode == null) {
			copyrightNode = settingElement.addElement("copyright");
		}
		copyrightNode.setText(setting.getCopyright());

		Node emailNode = document.selectSingleNode("/readboywx/setting/email");
		if (emailNode == null) {
			emailNode = settingElement.addElement("email");
		}
		emailNode.setText(setting.getEmail());

		Node assignOrder = document
				.selectSingleNode("/readboywx/setting/assignOrder");
		if (assignOrder == null) {
			assignOrder = settingElement.addElement("assignOrder");
		}
		assignOrder.setText(setting.getAssignOrder());

		Node img1Order = document.selectSingleNode("/readboywx/setting/img1");
		if (img1Order == null) {
			img1Order = settingElement.addElement("img1");
		}
		img1Order.setText(setting.getImg1() == null ? img1Order.getText()
				: setting.getImg1());

		Node img2Order = document.selectSingleNode("/readboywx/setting/img2");
		if (img2Order == null) {
			img2Order = settingElement.addElement("img2");
		}
		img2Order.setText(setting.getImg2() == null ? img2Order.getText()
				: setting.getImg2());

		Node img3Order = document.selectSingleNode("/readboywx/setting/img3");
		if (img3Order == null) {
			img3Order = settingElement.addElement("img3");
		}
		img3Order.setText(setting.getImg3() == null ? img3Order.getText()
				: setting.getImg3());

		Node adminimg1Order = document
				.selectSingleNode("/readboywx/setting/adminimg1");
		if (adminimg1Order == null) {
			adminimg1Order = settingElement.addElement("adminimg1");
		}
		adminimg1Order.setText(setting.getAdminimg1() == null ? adminimg1Order
				.getText() : setting.getAdminimg1());

		Node adminimg2Order = document
				.selectSingleNode("/readboywx/setting/adminimg2");
		if (adminimg2Order == null) {
			adminimg2Order = settingElement.addElement("adminimg2");
		}
		adminimg2Order.setText(setting.getAdminimg2() == null ? adminimg2Order
				.getText() : setting.getAdminimg2());

		Node adminimg3Order = document
				.selectSingleNode("/readboywx/setting/adminimg3");
		if (adminimg3Order == null) {
			adminimg3Order = settingElement.addElement("adminimg3");
		}
		adminimg3Order.setText(setting.getAdminimg3() == null ? adminimg3Order
				.getText() : setting.getAdminimg3());

		try {
			final OutputFormat outputFormat = OutputFormat.createPrettyPrint();// 设置XML文档输出格式
			outputFormat.setEncoding("UTF-8");// 设置XML文档的编码类型
			outputFormat.setIndent(true);// 设置是否缩进
			outputFormat.setIndent("	");// 以TAB方式实现缩进
			outputFormat.setNewlines(true);// 设置是否换行
			final XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(
					shopxxXmlFile), outputFormat);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 更新系统配置信息
	 * 
	 * @param setting
	 */
	public static void updateSetting(Setting setting) {
		writeSetting(setting);
	}
}